package picture

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/picture"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    pictureReq "github.com/flipped-aurora/gin-vue-admin/server/model/picture/request"
)

type PictureService struct {
}

// CreatePicture 创建Picture记录
// Author [piexlmax](https://github.com/piexlmax)
func (pictureService *PictureService) CreatePicture(picture picture.Picture) (err error) {
	err = global.GVA_DB.Create(&picture).Error
	return err
}

// DeletePicture 删除Picture记录
// Author [piexlmax](https://github.com/piexlmax)
func (pictureService *PictureService)DeletePicture(picture picture.Picture) (err error) {
	err = global.GVA_DB.Delete(&picture).Error
	return err
}

// DeletePictureByIds 批量删除Picture记录
// Author [piexlmax](https://github.com/piexlmax)
func (pictureService *PictureService)DeletePictureByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]picture.Picture{},"id in ?",ids.Ids).Error
	return err
}

// UpdatePicture 更新Picture记录
// Author [piexlmax](https://github.com/piexlmax)
func (pictureService *PictureService)UpdatePicture(picture picture.Picture) (err error) {
	err = global.GVA_DB.Save(&picture).Error
	return err
}

// GetPicture 根据id获取Picture记录
// Author [piexlmax](https://github.com/piexlmax)
func (pictureService *PictureService)GetPicture(id uint) (picture picture.Picture, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&picture).Error
	return
}

// GetPictureInfoList 分页获取Picture记录
// Author [piexlmax](https://github.com/piexlmax)
func (pictureService *PictureService)GetPictureInfoList(info pictureReq.PictureSearch) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&picture.Picture{})
    var pictures []picture.Picture
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Picture_commodity != nil {
        db = db.Where("picture_commodity = ?",info.Picture_commodity)
    }
    if info.Picture_position != "" {
        db = db.Where("picture_position = ?",info.Picture_position)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&pictures).Error
	return  pictures, total, err
}
